<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Event_c extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model(array('event_m', 'coordinator_m', 'client_m',
            'supplier_m', 'checklist_m', 'task_m','guest_m'));
    }

    function index($year = null, $month = null){
            $data['content'] = 'pages/events/event_calendar';
            $data['title'] = 'Event Calendar';
            $data['current_page'] = 'events-calendar-page';
            $data['class'] = '';
            $data['back_to'] = 'events';
            $data['calendar'] = $this->event_m->generate_calendar($year, $month);
            $data['coords'] = $this->coordinator_m->get_employees($this->session->userdata('accid'));
            $data['event_types'] = $this->event_m->get_event_types();
            $data['clients'] = $this->client_m->get_event_clients();
            $data['supplier'] = $this->supplier_m->get_supplier_id_name();
            $data['drag_category'] = $this->event_m->get_drag_category();
            $data['pics'] = $this->guest_m->get_pics();
            $this->load->view('includes/template', $data);
        }
        
        function list_view(){
            $data['content'] = 'pages/events/events_list_view';
            $data['title'] = 'Events List View';
            $data['current_page'] = 'events-list-page';
            $data['class'] = '';
            $data['back_to'] = 'events';
            $this->load->view('includes/template', $data);
            
        }
        function get_event(){
            $coord = $this->session->userdata('accid');
            $event_id = $this->input->post('event');
            $event = $this->event_m->get_event($event_id);
            $event_client = $this->event_m->get_client($event_id);
            $event_venue = $this->event_m->get_event_venue($event_id);
            $event_coord = $this->event_m->get_event_coord($event_id);
            $event_supplier = $this->event_m->get_event_supplier($event_id);
            $event_checklist = $this->event_m->get_event_checklist($event_id);
            $category = $this->checklist_m->get_all_category();
            $task = $this->task_m->get_task($this->session->userdata('accid'));
            $venues = $this->event_m->get_venues();
            
            echo json_encode(array('event'=>$event, 'event_client'=>$event_client,'event_supplier'=>$event_supplier, 
                'event_coord'=>$event_coord, 'event_venue'=>$event_venue, 'venues' => $venues,
                'event_checklist'=>$event_checklist, 'category'=>$category,'task'=>$task));
        }
        function get_checklist_item(){
            $checklist = $this->checklist_m->get_sub_cats($this->input->post('category_id'));
            echo json_encode($checklist);
        }
        
        function event_detail() {
            $data['content'] = 'pages/events/event_details';
            $data['title'] = 'Event Detail';
            $data['current_page'] = 'event-create-page';
            $data['class'] = '';
            $data['back_to'] = 'events';
            $data['event_types'] = $this->event_m->get_event_types();
            $data['event_venues'] = $this->event_m->get_venues();
            $data['coords'] = $this->coordinator_m->get_employees($this->session->userdata('accid'));
            $data['clients'] = $this->client_m->get_event_clients();
            $data['supplier'] = $this->supplier_m->get_all_supplier();
            $data['category'] = $this->checklist_m->get_all_category();
            $this->load->view('includes/template', $data);
        }
        
        function save_estimate_cost() {
            $status = '';
            $msg = '';
            $item = $this->input->post('item');
            $estimated_cost = $this->input->post('estimated_cost');
            $result = $this->event_m->insert_estimate_cost();
        }
        
        function event_task_ajax() {
            echo json_encode($this->task_m->get_all_tasks());
        }
        
        function save_task() {
            $status = '';
            $msg = '';
            $id = '';
            $data = array(
                'task_desc' => $this->input->post('task_desc'),
                'task_date' => $this->input->post('task_date'),
                'task_time' => $this->input->post('task_time'),
                'coord_id'=> $this->session->userdata('accid')
            );
            $result = $this->task_m->insert_task($data);
            if($result){
                $status = 'success';
                $msg = 'Task successfully added.';
                $id = $result;
            }else{
                $status = 'error';
                $msg = 'Something went wrong while saving the task.<br/>Please try again.';
                $id = '$result';
            }
            echo json_encode(array('status'=>$status,'msg'=>$msg,'task'=>$id));
        }
        
        function get_services(){
            $supplier_id = $this->input->post('supplier_id');
            $result = $this->event_m->get_services($supplier_id);
            echo json_encode($result);
        }
        
        function save_event(){
            $event_data = array(
                'event_name' => $this->input->post('event_name'),
                'initial_budget' => $this->input->post('event_budget'),
                'type_id' => $this->input->post('event_type'),
                'event_description' => $this->input->post('event_note'),
                'start_date' => $this->input->post('event_start_date'),
                'actual_date_time' => $this->input->post('event_actual_date'),
                'end_date' => $this->input->post('event_end_date'),
            );
            $coord_id = $this->input->post('event_coord');
            $client_id = $this->input->post('event_client');
            $venue_id = $this->input->post('event_loc');
            $event_checklist = $this->input->post('event_checklist');
            $event_supplier = $this->input->post('event_supplier');
            $event_task = $this->input->post('event_task');
            $user = $this->session->userdata('accid');
            $status = '';
            $msg = '';
            $result = $this->event_m->insert_event($event_data, $venue_id, $coord_id, $client_id, 
                    $event_checklist, $event_supplier, $event_task, $user);
            if($result>0){
                $status = 'success';
                $msg = 'Event successfully saved.';
            }else{
                $status = 'error';
               $msg = "Something wen't wrong while saving the event. Please try again.";
            }
            echo json_encode(array('status'=>$status,'msg'=>$msg));
        }
        
        function terminate_event(){
            $status = '';
            $event_id = $this->input->post('event');
            $result = $this->event_m->terminate_event($event_id);
            if($result>0){
                $status = 'success';
            }else{
                $status = 'error';
            }
            echo json_encode($status);
        }
        
        function cancel_event(){
            $status = '';
            $event_id = $this->input->post('event');
            $result = $this->event_m->cancel_event($event_id);
            if($result>0){
                $status = 'success';
            }else{
                $status = 'error';
            }
            echo json_encode($status);
        }
        
        function update_event_details(){
            $status = '';
            $msg = '';
            $event_id = $this->input->post('event');
            $event_data = array(
                'event_name' => $this->input->post('event_name'),
                'initial_budget' => $this->input->post('event_budget'),
                'type_id' => $this->input->post('event_type'),
                'event_description' => $this->input->post('event_note'),
                'start_date' => $this->input->post('event_start_date'),
                'end_date' => $this->input->post('event_end_date'),
                'actual_date_time' => $this->input->post('actual_date'),
            );
            $coord_id = $this->input->post('event_coord');
            $client_id = $this->input->post('event_client');
            $venue_id = $this->input->post('event_loc');
            $result = $this->event_m->update_event_details($event_id, $event_data, 
                    $coord_id, $client_id, $venue_id);
            if($result>0){
                $status = 'success';
                $msg = 'Event details successfully saved.';
            }else{
                $status = 'error';
                $msg = 'Nothing was updated.';
            }
            echo json_encode(array('status'=> $status, 'msg'=>$msg, 'array'=>is_array($venue_id)));
        }
        
        function update_event_checklist(){
            $status = '';
            $msg = '';
            $event_id = $this->input->post('event');
            $checklist = $this->input->post('checklist');
            $result = $this->event_m->update_event_checklist($event_id, $checklist);
            if($result>0){
                $status = 'success';
                $msg = 'Checklist successfully update.';
            }else{
                $status = 'error';
                $msg = 'Nothing updated.';
            }
            echo json_encode(array('status'=>$status, 'msg'=>$msg));
        }
        
        function save_checklist_item(){
            $status = '';
            $msg = '';
            $checklist = array(
                'checklist_id'=>$this->input->post('checklist_id'),
                'event_id'=>$this->input->post('event'),
                'checklist_status'=>'',
                'ch_remarks'=>'',
            );
            $result = $this->event_m->insert_event_checklist($checklist);
            if($result>0){
                $status = 'success';
                $msg = 'Checklist item successfully added.';
            }else{
                $status = 'error';
                $msg = 'Something went wrong while adding the item.<br/>Please try again.';
            }
            echo json_encode(array('status'=>$status, 'msg'=>$msg));
        }
        
        function add_event_supplier(){
            $status = '';
            $msg = '';
            $event_supplier = array(
                'event_id' => $this->input->post('event'),
                'supplier_id' => $this->input->post('supplier_id'),
                'event_svc' => $this->input->post('services'),
            );
            $result = $this->event_m->add_event_supplier($event_supplier);
            if($result>0){
                $status = 'success';
                $msg = 'Supplier and services successfully saved.';
            }else{
                $status = 'error';
                $msg = "Something wen't wrong while saving the supplier and services.<br/>Please try again.";
            }
            echo json_encode(array('status'=>$status,'msg'=>$msg));
        }
        
        function remove_event_supplier(){
            $status = '';
            $msg = '';
            $supplier_id = $this->input->post('supplier_id');
            $result = $this->event_m->remove_event_supplier($supplier_id);
            if($result>0){
                $status = 'success';
                $msg = 'Supplier successfully removed.';
            }else{
                $status = 'success';
                $msg = "Something wen't wrong while removing supplier.<br/>Please try again.";
            }
            echo json_encode(array('status'=>$status,'msg'=>$msg));
        }
        
        function update_services(){
            $status = '';
            $msg = '';
            $event = $this->input->post('event');
            $supplier_id = $this->input->post('supplier_id');
            $services = $event_id = $this->input->post('services');
            $result = $this->event_m->update_event_services($event, $supplier_id, $services);
            if($result>0){
                $status = 'success';
                $msg = 'Services successfully updated.';
            }else{
                $status = 'error';
                $msg = 'Something went wrong while updating services.<br/>Services unsuccessfully updated.<br/>Please try again.';
            }
            echo json_encode(array('status'=>$status, 'msg'=>$msg));
        }
        
        function delete_task(){
            $status = '';
            $msg = '';
            $task_id = $this->input->post('task_id');
            $result = $this->task_m->delete_task($task_id);
            if($result>0){
                $status = 'success';
                $msg = 'Task successfully removed.';
            }else{
                $status = 'error';
                $msg = 'Something went wrong while removeing task.<br/>Please try again.';
            }
            echo json_encode(array('status'=>$status, 'msg'=>$msg));
        }

    function save_layout() {
        $hasLay = $this->event_m->has_layout($this->input->post("ev-id"));
        if ($hasLay) {
            $this->event_m->update_main_layout(1, $this->input->post("imgData"));
            $data['floorId'] = 1;
            echo "updated";
        } else {
            $query = $this->event_m->get_max_main_layout_id();
            if ($query->num_rows() > 0) {
                $row = $query->row();
                $data['floorId'] = $row->main_layout_id + 1;
            } else {
                $data['floorId'] = 1;
            }
            if ($_FILES['main-layout-img']['name']) {
                if (!$_FILES['main-layout-img']['error']) {
                    $ext = end(explode(".", $_FILES['main-layout-img']['name']));
                    $data['imgLoc'] = "uploads/layout/main-" . $data['floorId'] . "." . $ext;
                    move_uploaded_file($_FILES['main-layout-img']['tmp_name'], $data['imgLoc']);
                    //$this->event_m->save_main_bg($data['floorId'],$data['imgLoc']);
                } else {
                    echo "invalid file";
                }
            }
            $this->event_m->save_main_layout($data['floorId'], $this->input->post("imgData"), $this->input->post("ev-id"), $data['imgLoc']);
            echo $data['floorId'];
        }
    }

    function save_sub_layout() {
        $res = $this->event_m->get_main_layout_id($this->input->post("ev-id"));
        if ($res->num_rows() > 0) {
            $row = $res->row();
            $id = $row->main_layout_id;
        }
        $this->event_m->save_sub_lay($this->input->post("sub-id"), $this->input->post("subData"), $id);
    }

    function update_sub_layout() {
        $res = $this->event_m->get_main_layout_id($this->input->post("ev-id"));
        if ($res->num_rows() > 0) {
            $row = $res->row();
            $id = $row->main_layout_id;
        }
        $this->event_m->update_sub_lay($this->input->post("sub-id"), $this->input->post("subData"), $id);
    }

    function sub_reload_layout() {
        $res = $this->event_m->get_main_layout_id($this->input->post("ev-id"));
        if ($res->num_rows() > 0) {
            $row = $res->row();
            $id = $row->main_layout_id;
        }

        $query = $this->event_m->get_sub_layout($this->input->post("sub-id"), $id);
        if ($query->num_rows() > 0) {
            $r = $query->row();
            $data = $r->sub_layout_data;
        }
        echo json_encode($data);
    }

    function check_has_layout() {
        $hasLay = $this->event_m->has_layout($this->input->post("ev-id"));
        $query = $this->event_m->check_floor_count($this->input->post("ev-id"));
        $singleOrMultiple = "";
        if ($query->num_rows() > 0) {
            $row = $query->row();
            if($row->main_layout_data == "blank" || $row->main_img_url == "blank"){
                $singleOrMultiple = "single";
                $id = $row->main_layout_id;
            }else{
                $singleOrMultiple = "multiple";
            }
        }
        
        if ($hasLay) {
            if($singleOrMultiple == "multiple"){
                echo json_encode(array("status"=>1,"subId"=>""));
            }else{
                $subId = $this->event_m->has_sub_layout($id);
                echo json_encode(array("status"=>2,"subId"=>$subId));
            }
            
        } else {
            echo json_encode(array("status"=>0,"subId"=>""));
        }
    }

    function check_sub_has_layout() {
        $res = $this->event_m->get_main_layout_id($this->input->post("ev-id"));
        if ($res->num_rows() > 0) {
            $row = $res->row();
            $id = $row->main_layout_id;
        }

        $hasLay = $this->event_m->sub_has_layout($this->input->post("sub-id"), $id);
        if ($hasLay) {
            echo json_encode(1);
        } else {
            echo json_encode(0);
        }
    }

    function reload_layout() {
        $result = $this->event_m->get_layout($this->input->post("ev-id"));
        if ($result->num_rows() > 0) {
            $row = $result->row();
            $data = array(
                'imgData' => $row->main_layout_data,
                'imgUrl' => $row->main_img_url
            );
        }
        echo json_encode($data);
    }
    
    function get_tools(){
        $res = "";
        $query = $this->event_m->get_drag_img($this->input->post("tool-type"));
        foreach ($query->result() as $row){
            $size = getimagesize(site_url().$row->drag_image_url);
            $res = $res . '<div><img src="'.  site_url().$row->drag_image_url.'" data-tooltip="'.$row->drag_image_tooltip.'" data-width="'.  $size[0].'" data-height="'.$size[1].'"/></div>';
        }
        echo json_encode($res);
    }
    
    /***Production Schedule****/
    function prodsched_save(){	
            $prod = $_POST['descr'];
            $pid = $_POST['pid'];
            $id = $this->event_m->insert_prodsched($prod,$pid);
            if($id->num_rows()>0){
                $result = $id->row();
                $data = $result->description;
                $status = 'Schedule successfully saved';
            }else{
              $status = 'error';
              $data = 'No Saved Schedule';
            }
            echo json_encode(array('status' => $status, 'result' => $data));       
         }
        function prodsched_update(){
            $produ =  $_POST['desc'];          
            $prod_id = $_POST['id'];
            $id = $this->event_m->update_prodsched($produ,$prod_id);            
            if($id->num_rows()>0){
                $res = $id->row();
                $data = $res->description;
                $status = 'Schedule successfully updated';
            }else{
                $res = $id->row();
                $data = $res->description;
                $status = 'error';
            }
            echo json_encode(array('status' => $status, 'result' => $data));  
        }
        function prodsched_view(){
            $event_id = $_POST['schedule'];
            $result = $this->event_m->get_prodsched($event_id);         
            if($result->num_rows() >0){
              $res = $result->row();
              $data= $res->description;
              $status = 'success';  
            }else{
              $status = 'error';
              $data = 'No Saved Schedule';
            }
              echo json_encode(array('status' => $status, 'result' => $data)); 
        }
        function exportPDF(){
            date_default_timezone_get();
            $sysdate = date('Y/m/d H:i:s');
            $eventname = $_POST['name'];
            $data = '';
            $event_id = $_POST['pid'];
            $result = $this->event_m->get_prodsched($event_id);         
            if($result->num_rows() >0){
              $res = $result->row();
              $desc= $res->description;
              $data = '<p style="text-align:center">'.$eventname.'</p>
                       <p style="text-align:right"> Expense List As of '.$sysdate.'</p> <br />'
                       .$desc;
              $status = 'success';  
            }else{
              $status = 'error';
              $data = 'No Saved Schedule';
            }
              echo json_encode(array('status' => $status, 'result' => $data)); 
        }
        
        function save_single_floor(){
            $hasLay = $this->event_m->has_layout($this->input->post("ev-id"));
            if ($hasLay) {
                $this->event_m->update_main_layout(1, "blank");
                $data['floorId'] = 1;
            } else {
                $query = $this->event_m->get_max_main_layout_id();
                if ($query->num_rows() > 0) {
                    $row = $query->row();
                    $data['floorId'] = $row->main_layout_id + 1;
                } else {
                    $data['floorId'] = 1;
                }
                $this->event_m->save_main_layout($data['floorId'], "blank", $this->input->post("ev-id"), "blank");
            }
            echo json_encode("pagSuccess");
        }
}
